'use strict';
const base = require('../base/serviceBase');
const _ = require('lodash');
const moment = require('moment');
const _stringUtils = require('../utils/stringUtils');

module.exports = class extends base {
  async createDic(body) {
    const { ctx } = this;
    const { tenantCode, recordId, treeId, SFGZ } = body;
    body.recordId ? recordId : _stringUtils.getCode('RID');
    const { table } = body;
    const result = await ctx.app.dbs.insert(table, body);
    /**
     * 同步工资
     */
    // if (treeId === 'DicPerPayroll') {
    //   const payload = { tenantCode, paymentAmount: SFGZ, };
    // }
    return { status: 'ok', result };
  }

  async findDicMany(body) {
    const { ctx, service } = this;
    const { table, tenantCode } = body;
    const query = { tenantCode };
    delete body['tenantName'];
    delete body['tenantCode'];
    const schema = await service.globalService.getModel({ table });

    _.map(body, (value, key) => {
      const temp = _.find(schema, (it, k) => {
        return k === key;
      });

      if (temp) {
        const { _type } = temp;
        query[key] = this.getValue(_type, value, key);
      }
    });
    const result = await ctx.app.dbs.find(table, query);
    return { status: 'ok', result };
  }

  async exportExcel(body) {
    const { ctx, service } = this;
    const { table, tenantCode } = body;
    const query = { tenantCode };
    delete body['tenantName'];
    delete body['tenantCode'];
    const schema = await service.globalService.getModel({ table });

    _.map(body, (value, key) => {
      const temp = _.find(schema, (it, k) => {
        return k === key;
      });

      if (temp) {
        const { _type } = temp;
        query[key] = this.getValue(_type, value, key);
      }
    });
    const data = await ctx.app.dbs.find(table, query);

    const mod = ctx.model[table];
    const _header = mod.schema.obj;
    const header = [];
    _.map(_header, (item, key) => {
      item.dataIndex = key;
      item.key = key;
      header.push(item);
    });
    return { status: 'ok', result: { header, data } };

  }
};
